import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xvideos/assets_tool/images.dart';
import 'package:xvideos/hj_model/home/home_tabs_entity.dart';
import 'package:xvideos/hj_model/home/module_detail_model.dart';
import 'package:xvideos/hj_page/home_business/section_all_page/section_all_page.dart';
import 'package:xvideos/hj_page/home_business/special_topic_detail/specail_topics_detail_page.dart';
import 'package:xvideos/hj_utils/widget_utils.dart';

import '../../../../tools_base/widget/net_image_widget.dart';
import '../../../cartoon_business/cartoon_sectionlist_page.dart';
import '../../tag/carrtoon_tag_page.dart';

class SpecialTopicsView extends StatefulWidget {
  final List<AllSection>? specials;
  final ModuleData? moudle;
  final EdgeInsetsGeometry? padding;
  final bool isDarkStyle;
  final bool isACG;

  const SpecialTopicsView(
    this.specials, {
    Key? key,
    this.moudle,
    this.padding,
    this.isDarkStyle = false,
    this.isACG = false,
  });

  @override
  State<SpecialTopicsView> createState() => _SpecialTopicsViewState();
}

class _SpecialTopicsViewState extends State<SpecialTopicsView> {
  int? get showType => widget.moudle?.showType;

  @override
  Widget build(BuildContext context) {
    if (widget.specials?.isEmpty ?? true) return SizedBox.shrink();

    if (showType == 2 || showType == null || showType == 0) {
      return GridView.builder(
        shrinkWrap: true,
        padding: widget.padding,
        physics: NeverScrollableScrollPhysics(),
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 4,
          mainAxisSpacing: 10,
          crossAxisSpacing: 9,
          childAspectRatio: 82 / 33,
        ),
        itemCount: widget.specials?.length ?? 0,
        itemBuilder: (BuildContext context, int index) {
          return _buildItem(index);
        },
      );
    }
    if (showType == 4) {
      return _buildTagsWidget();
    }
    if (showType == 5) {
      // 女优网黄展示样式
      return _buildActressWidget();
    }
    return SizedBox();
  }

  Widget _buildItem(int index) {
    final AllSection model = widget.specials![index];
    return GestureDetector(
      onTap: () {
        if (widget.moudle?.isACG == true) {
          Get.to(CartoonSectionListPage(
            sectionID: model.sectionID,
            tagName: model.sectionName,
          ));
        } else {
          Get.to(SpecailTopicsDetailPage(model), preventDuplicates: false);
        }
      },
      child: Container(
        decoration: BoxDecoration(
          color: widget.isDarkStyle ? Color(0x4D810906) : Color(0x0DFFFFFF),
          borderRadius: BorderRadius.all(Radius.circular(2)),
        ),
        width: double.infinity,
        height: double.infinity,
        child: Stack(
          alignment: Alignment.center,
          children: [
            if (widget.isDarkStyle)
              Positioned(
                top: 0,
                child: Image.asset("aw_tag.webp".homePath, height: 13),
              ),
            Text(
              "${model.sectionName}",
              style: TextStyle(
                color: Color(0xE5FFFFFF),
                fontSize: 12,
                fontWeight: FontWeight.w500,
              ),
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
            )
          ],
        ),
      ),
    );
  }

  double itemTagHeight = 77;

  Widget _buildTagsWidget() {
    return Container(
      margin: widget.padding,
      height: itemTagHeight,
      child: LayoutBuilder(builder: (context, constraints) {
        double itemWidth = (constraints.maxWidth - 12 * 5) / 5.2;
        if (itemTagHeight != itemWidth + 22) {
          itemTagHeight = itemWidth + 22;
          WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
            setState(() {});
          });
        }
        return ListView.separated(
          itemCount: widget.specials?.length ?? 0,
          scrollDirection: Axis.horizontal,
          padding: EdgeInsets.zero,
          separatorBuilder: (_, __) => SizedBox(width: 12),
          itemBuilder: (BuildContext context, int index) {
            final AllSection model = widget.specials![index];
            return GestureDetector(
              onTap: () {
                ///进入标签页
                Get.to(SpecailTopicsDetailPage(model), preventDuplicates: false);
              },
              child: SizedBox(
                height: double.infinity,
                width: itemWidth,
                child: Column(
                  children: [
                    NetworkImageLoader(
                      imageUrl: model.sectionCover ?? '',
                      width: itemWidth,
                      height: itemWidth,
                      borderRadius: itemWidth,
                    ),
                    SizedBox(height: 2),
                    Text(
                      model.sectionName ?? '',
                      style: TextStyle(
                        color: Color(0xE5FFFFFF),
                        fontSize: 12,
                      ),
                      maxLines: 1,
                    )
                  ],
                ),
              ),
            );
          },
        );
      }),
    );
  }

  double itemActressHeight = 77;

  Widget _buildActressWidget() {
    return Container(
      margin: widget.padding,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            child: InkWell(
              onTap: () {
                Get.to(SectionAllPage(widget.moudle?.id ?? ""));
              },
              child: Row(
                children: [
                  Expanded(
                    child: Text(
                      '${widget.specials?.first.sectionName ?? ""}',
                      maxLines: 1,
                      style: TextStyle(
                        color: Color(0xE5FFFFFF),
                        fontSize: 18,
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                  ),
                  10.sizeBoxW,
                  Text(
                    '更多',
                    maxLines: 1,
                    style: TextStyle(
                      color: Color(0x59FFFFFF),
                      fontSize: 12,
                    ),
                  ),
                  Image.asset("arrow_right_grey.webp".commonImgPath, width: 18),
                ],
              ),
            ),
          ),
          12.sizeBoxH,
          Container(
            height: itemActressHeight,
            child: LayoutBuilder(
              builder: (context, constraints) {
                double itemWidth = (constraints.maxWidth - 12 * 5) / 5.5;
                if (itemActressHeight != itemWidth + 22) {
                  itemActressHeight = itemWidth + 22;
                  WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
                    setState(() {});
                  });
                }
                return ListView.separated(
                  itemCount: widget.specials?.length ?? 0,
                  scrollDirection: Axis.horizontal,
                  padding: EdgeInsets.zero,
                  separatorBuilder: (_, __) => SizedBox(width: 12),
                  itemBuilder: (BuildContext context, int index) {
                    final AllSection model = widget.specials![index];
                    return GestureDetector(
                      onTap: () {
                        ///进入标签页
                        Get.to(SpecailTopicsDetailPage(model), preventDuplicates: false);
                      },
                      child: SizedBox(
                        height: double.infinity,
                        width: itemWidth,
                        child: Column(
                          children: [
                            NetworkImageLoader(
                              imageUrl: model.sectionCover ?? '',
                              width: itemWidth,
                              height: itemWidth,
                              borderRadius: itemWidth,
                            ),
                            SizedBox(height: 2),
                            Text(
                              model.sectionName ?? '',
                              style: TextStyle(
                                color: Color(0xE5FFFFFF),
                                fontSize: 12,
                              ),
                              maxLines: 1,
                            )
                          ],
                        ),
                      ),
                    );
                  },
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}
